const app = getApp<IAppOption>()

Page({
  data: {
    setting: {
      title: '',
      logo: '',
    },
    shareTitle: '',
    url: '',
  },

  onLoad(options: any) {
    wx.showLoading({ title: '加载中' });
    let url = app.siteInfo.baseUrl + '/h5/digit_expo/' + app.siteInfo.uniacid + '/index'
    if (options.url) {
      url = decodeURIComponent(options.url)
    }

    let shareTitle = ''
    if (options.title) {
      shareTitle = options.title
    }

    this.setData({ shareTitle, url })
  },

  onShow() {
    wx.showShareMenu({ withShareTicket: true });

    // if (!this.data.setting.title) {
    //   wx.request({
    //     url: 'entry/wxapp/setting',
    //     method: 'GET',
    //     header: {
    //       'content-type': 'application/x-www-form-urlencoded',
    //     },
    //     success: (res: any) => {
    //       this.setData({ setting: res.data.data })
    //       // wx.hideLoading();
    //     },
    //     fail: () => {
    //       // wx.hideLoading();
    //     }
    //   })
    // }
  },

  onShareAppMessage(options: any) {
    const webViewUrl = '/pages/web-view/index?url=' + encodeURIComponent(options.webViewUrl) + '&title=' + decodeURIComponent(this.data.shareTitle)

    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: this.data.shareTitle,
          path: webViewUrl
        })
      }, 300)
    })

    return {
      title: this.data.setting.title,
      imageUrl: this.data.setting.logo,
      path: webViewUrl,
      promise
    }
  },

  // 页面加载情况
  onBindMessage(e: any) {
    // 延时隐藏 loading
    setTimeout(() => wx.hideLoading(), 500);

    if (e.type === 'load') {
      // 加载成功
    } else if (e.type === 'error') {
      // 加载失败
    }
  },
})